
const { redisSet, redisGet } = require('../libs/redisClient');
const { returnInfo, addProblemType } = require('../libs/utils'),
      { API } = require('../config/error_config');

const pageConf = require('../config/config').pageConf;
const commonPageConf = require('../config/config').commonPageConf;

//service
const ProblemService = require('../services/Problem');
const ProblemTypeService = require('../services/ProblemType');

class IndexController {

  async index (ctx, next) {
    const sess = ctx.session;

    let problemList = await ProblemService.getAllProblems();

    //把problemTypeId -> type 类型字段
    problemList = await addProblemType(problemList);


    // 渲染模板
    await ctx.render('user/index', {
      pageConf: pageConf.index,
      commonPageConf,
      problemList,
      isLogin: ctx.session.userInfo ? true : false,
      curRouter: '/index',
      rankingList: [
        {
          user_name: '小小',
          pass_times: 1200,
          online_time: '325',
          regist_time: '2020.02.03'
        },
        {
          user_name: '小林',
          pass_times: 1200,
          online_time: '325',
          regist_time: '2020.02.03'
        },
        {
          user_name: '小廖',
          pass_times: 1200,
          online_time: '325',
          regist_time: '2020.02.03'
        },
      ]
    })
  }

  //详情
  async getProblemDetail (ctx, next) {

    const pid = ctx.params.pid;
    const problem = await ProblemService.findProblemById(pid);

    await ctx.render('user/problem_detail', {
      pageConf: pageConf.problemDetail,
      commonPageConf,
      problem,
      isLogin: ctx.session.userInfo ? true : false,
      curRouter: '/problem-detail',
    })
  }

  //题库页面
  async getProblemStorePage (ctx, index) {

    let problemList = await ProblemService.getAllProblems();

    //把problemTypeId -> type 类型字段
    problemList = await addProblemType(problemList);


    await ctx.render('index/problem_store', {
      pageConf: pageConf.problemStore,
      commonPageConf,
      problemList,
      isLogin: ctx.session.userInfo ? true : false,
      curRouter: '/problem-store',
    })
  }

 
}

module.exports = new IndexController();